var express = require('express');
var router = express.Router();
var userDAO = require("../bin/userDAO.js")
var md5 = require("md5");
/* GET users listing. */
var multer = require("multer");
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, './public/images/avatar')
    },
    filename: function (req, file, cb) {
      var suffix = file.mimetype.split("/")[1];
      var filename = file.fieldname + '-' + req.session.user._id+"."+suffix;
      req.userAvatar = filename;
      cb(null, filename);
    }
})
var upload = multer({ storage: storage });

// 注册接口
router.post("/regist",function(req,res){
    userDAO.findOne({name:req.body.name}).then(data1=>{
      if(data1){
        res.json({err:1,msg:"此用户名已存在"})
      }else{
        req.body.psw = md5(req.body.psw);
        var u = new userDAO(req.body);
        u.save().then(data=>{
          res.json({err:0,msg:"注册成功"})
        })
      }
    })
})

// 登录接口
router.post("/login",function(req,res){
    userDAO.findOne({name:req.body.name}).then(data1=>{
      if(data1){
        if(md5(req.body.psw)==data1.psw){
          req.session.user = data1;
          res.json({err:0,msg:"登录成功",data:data1})
        }else{
          res.json({err:1,msg:"密码错误"})
        }
      }else{
        res.json({err:2,msg:"账户不存在"})
      }
    })
})

// 上传头像
router.post("/avatar",upload.single("avatar"),function(req,res){
  userDAO.findOne({name:req.session.user.name}).then(data1=>{
    data1.avatar = req.userAvatar;
    data1.save().then(data=>{
      req.session.user.avatar = data.avatar;
      res.json({err:0,msg:"上传成功",data:data});
    })
  })
})

//添加最近阅读
router.get("/addRead",(req,res)=>{
  var a = -2;
  userDAO.findOne({_id:req.session.user._id}).then(data1=>{
    data1.read.forEach(function(e,i) {
      if(e==req.query.id){
        a = i;
      }
    });
    if(a==-2){
      data1.read.push(req.query.id);
      data1.readin.push(req.query.readin);
    }else{
      data1.readin.splice(a,1,req.query.readin);
    }
    data1.save().then(data=>{
      res.json({err:0,msg:"网站维护中，暂时无法阅读！"})
    })
  })
})
// 获取最近阅读
router.get("/getRead",(req,res)=>{
  userDAO.findOne({_id:req.session.user._id}).populate("read")
  .then(data=>{
    res.json({err:0,data:data.read,readin:data.readin})
  })
})


// 添加收藏
router.get("/addcollections",(req,res)=>{
  userDAO.findOne({_id:req.session.user._id}).then(data1=>{
    for(var i = 0;i<data1.collections.length;i++){
      if(data1.collections[i]==req.query.id){
        res.json({err:0,msg:"您已经添加过该书了"})
        return;
      }
    }
    data1.collections.push(req.query.id);
    data1.save().then(data=>{
      res.json({err:0,msg:"添加成功"})
    })
  })
})
// 查找收藏
router.get("/getcollections",(req,res)=>{
  userDAO.findOne({_id:req.session.user._id}).populate("collections").then(data=>{
    res.json({err:0,data:data.collections})
  })
})
// 删除收藏
router.get("/delcollections",(req,res)=>{
  userDAO.findOne({_id:req.session.user._id}).then(data1=>{
    removeArr(data1.collections,req.query.id);
    data1.save().then(data=>{
      res.json({err:0,msg:"删除成功"})
    })
  })
})
function removeArr(Arr,val){
  var index = Arr.indexOf(val);
  if(index>-1){
    Arr.splice(index,1);
  }
}
// 退出登录
router.get("/tui",function(req,res){
  if(req.session.user){
    req.session.user = null;
    res.json({err:0,msg:"退出成功"})
  }else{
    res.json({err:1,msg:"请先登录"})
  }
})

// 判断是否登录
router.get("/islogin",function(req,res){
  if(req.session.user){
    res.json({err:0,data:req.session.user})
  }else{
    res.json({err:1})
  }
})


module.exports = router;
